package org.looop.chuck;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import junit.framework.TestCase;

import org.looop.chuck.domain.AppInfo;
import org.looop.chuck.parser.HTMLParserFactory;
import org.looop.chuck.parser.impl.DefaultHTMLParserFactory;
import org.looop.chuck.persistence.PersistenceManager;
import org.looop.chuck.persistence.impl.FileAppInfoWriter;
import org.looop.chuck.persistence.impl.MemoryPersistenceManager;
import org.looop.chuck.schedule.URLTaskScheduler;
import org.looop.chuck.schedule.impl.DefaultScheduler;

public class HiApkCrawl extends TestCase {
	public void testCrawl() throws Exception{
		MemoryPersistenceManager memPersistenceManager = new MemoryPersistenceManager("/tmp/hiApk.tmp", "utf-8");
		String mainAppEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=45&currentHash=1_1_0_0_0_0_0";
		String imEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=49&currentHash=1_1_0_0_0_0_0";
		String mediaEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=39&currentHash=1_1_0_0_0_0_0";
		String officeEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=46&currentHash=1_1_0_0_0_0_0";
		String dictionaryEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=36&currentHash=1_1_0_0_0_0_0";
		String travelEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=42&currentHash=1_1_0_0_0_0_0";
		String systoolEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=52&currentHash=1_1_0_0_0_0_0";
		String lifeEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=37&currentHash=1_1_0_0_0_0_0";
		String desktopEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=35&currentHash=1_1_0_0_0_0_0";
		String readingEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=71&currentHash=1_1_0_0_0_0_0";
		String otherEntry = "http://apk.hiapk.com/App.aspx?action=FindAppSoftList&categoryId=40&currentHash=1_1_0_0_0_0_0";
		
		String[] appEntries = new String[]{
				mainAppEntry,
				imEntry,
				mediaEntry,
				officeEntry,
				dictionaryEntry,
				travelEntry,
				systoolEntry,
				lifeEntry,
				desktopEntry,
				readingEntry,
				otherEntry
		};
		
		String mainGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&currentHash=1_1_0_0_0_0_0";
		String actionGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=31&currentHash=1_1_0_0_0_0_0";
		String rpgGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=79&currentHash=1_1_0_0_0_0_0";
		String shootGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=80&currentHash=1_1_0_0_0_0_0";
		String racingGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=81&currentHash=1_1_0_0_0_0_0";
		String ployGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=29&currentHash=1_1_0_0_0_0_0";
		String cheeseGameEntry = "http://apk.hiapk.com/Game.aspx?action=FindGameSoftList&categoryId=30&currentHash=1_1_0_0_0_0_0";
		
		String[] gameEntries = new String[]{
				mainGameEntry,
				actionGameEntry,
				rpgGameEntry,
				shootGameEntry,
				racingGameEntry,
				ployGameEntry,
				cheeseGameEntry
		};
		
		
		ExecutorService exec = Executors.newCachedThreadPool();
		HTMLParserFactory htmlParserFactory = new DefaultHTMLParserFactory();
		URLTaskScheduler scheduler = new DefaultScheduler(memPersistenceManager, exec, htmlParserFactory);
		
		for(String url : gameEntries){
			scheduler.submit(url);
		}
		
		scheduler.awaitFinish();
		exec.shutdown();
		PersistenceManager filePersistenceManager = new FileAppInfoWriter("/tmp/hiApkGameInfo.csv", "utf-8");
		List<AppInfo> appInfoList = new ArrayList<AppInfo>();
		appInfoList.addAll(memPersistenceManager.getMemStorage().values());
		filePersistenceManager.writeAppInfo(appInfoList);
		filePersistenceManager.release();
		memPersistenceManager.release();
		System.out.println("shutdown");
	}
}
